'use strict';
/* 代码在线工具 */
define( function(){
	// sql格式化
	var SqlFormatCtrl = ['$scope', '$http', function($scope, $http){
					
		$scope.code = "";
		// 执行代码
		$scope.run = function(){
			var sql=$.trim($scope.code);
			if(sql == ""){
				alert("请编写要执行的SQL");
			}
			var reg=/\s{2,}/g;
		  	sql=sql.replace(reg," ");  
			var para={"sql":sql};
			$http({	method:'post',
				url:"tools/sqlFormat",
				params:para
			}).success(function(result){
				if(result.status == 1){
					$scope.code=result.msg;
				}else{
					alert(result.msg);
				}
			}).error(function(){
				alert("网络连接失败");
			});
		};
		$scope.clear = function(){
			$scope.code="";
		};
		
		$scope.themes = ['default', 'blackboard','cobalt','eclipse','elegant','twilight'];
		$scope.currTheme = 'default';
		$scope.changeTheme = function(){
			
			if($scope.currTheme != 'default'){
				$("#cm_theme").attr("href", "res/plugins/codemirror/theme/"+$scope.currTheme+".css");
			}
			$scope.cm.setOption("theme", $scope.currTheme);
			$(".runner-panel").find(".runner-result").addClass($(".runner-code .CodeMirror").attr("class"));
		};
		// js 日志打印函数
		function log(result){
			 var text = $("#result").html();
		 if(text){
			 text += "<br/>";
		 } 
		 $("#result").html(text+ "<span style='color:blue;font-size:12px;'><strong>>></strong></span>"+ result);
		}
 	}];
	
	return {sqlFormat: SqlFormatCtrl};
});
